const app = getApp();
import {chatDetail,chatComment,chatSpeakGood} from "../../../config/api";
import util from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSpeak:false,//是否展示评论展示框，默认不展示
    content:'',//评论的内容数据为
    isVideo:false,//是否显示video样式
    last_id:null,//上一个页面传递过来的id
    chatDetail:{},//村聊的详情
  },

    //获得内容
    getContent(e){
      let that = this;
      console.log('获取的输入框内容：',e.detail.value);
      that.setData({
        content:e.detail.value
      });
    },

    //点击评论
toSpeak(){
  let that = this;
  console.log('点击了评论按钮！');
  that.setData({
    isSpeak:true
  });
},

//发布评论
faBu(){
  let that = this;
  console.log('点击了发布按钮！');
  let member = wx.getStorageSync('member');
  if(that.data.content.length==0){
    that.showDialogTitle('评论不能为空！')
    return;
  }
  if(!member){
    that.showDialogTitle(app.globalData.info);
    return;
  }
  let params = {
    idcard: member.idcard,
    talk_id: that.data.last_id,
    pid: 0,
    content: that.data.content,
    user_name: member.name,
    address: '',
    phone: member.mobile,
    headimg:member.heard_img?member.heard_img:'',
  };
  console.log('提交的参数params：',params);
  util.apiRequest(chatComment,params).then(r=>{
    console.log('提交的评论结果为,r');
    that.getDetail(that.data.last_id)
    that.setData({
      isSpeak:false
    });
  });
},


//点击点赞
toSpeakGood(){
  let that= this;
  let member = wx.getStorageSync('member');
  if(!member){
    that.showDialogTitle(app.globalData.info);
    return;
  }
  let params = {
    idcard: member.idcard,
    talk_id: that.data.last_id,
    user_name: member.name,
    phone: member.mobile,
    address: '',
  };
  console.log('点击了点赞按钮！',params);
  util.apiRequest(chatSpeakGood,params).then(r=>{
   console.log('点赞结果：',r);
   that.getDetail(that.data.last_id)
  });
},



//预览图片
previewImg(e){
  let that = this;
  console.log('e.currentTarget.dataset.img:',e.currentTarget.dataset.img);
  wx.previewImage(
    {
      current: e.currentTarget.dataset.img,
      urls: e.currentTarget.dataset.imgs
    }
  );
},


// 通用的提示框
showDialogTitle(title){
  wx.showModal({
    title: '提示',
    content: title,
    showCancel:false,
    success (res) {
      if (res.confirm) {
        console.log('用户点击确定');
        return;
      } 
    }
  })
},//展示相应的弹窗框提示二次封装


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('上一个页面传递过来的id：',options.id);
    this.setData({
      last_id:options.id
    });
    this.getDetail(options.id);
  },

  //获得村聊详情
  getDetail(id){
      let that = this;
      let member = wx.getStorageSync('member')
      let params = {
        id: id,
      };  
      if(member){
         params={
          id: id,
          idcard: member.idcard
         }
      }

      util.apiRequest(chatDetail,params,null).then(r=>{
        console.log('获取到的详情为：',r.data.data);
        that.setData({
          chatDetail:r.data.data
        });
      });


  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})